package com.xmtlxx.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.wms.domain.bo.CabinetGroupBo;
import com.xmtlxx.wms.domain.bo.StoreAreaBo;
import com.xmtlxx.wms.domain.vo.CabinetGroupVo;
import com.xmtlxx.wms.domain.vo.StoreAreaVo;
import com.xmtlxx.wms.service.ICabinetGroupService;
import com.xmtlxx.wms.service.IStoreAreaService;
import com.xmtlxx.wms.service.IStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 智能柜Controller
 *
 * @author xmtlxx
 * @date 2022-12-15
 */
@Validated
@Api(value = "智能柜控制器", tags = {"智能柜管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/cabinetGroup")
public class CabinetGroupController extends BaseController {

    private final ICabinetGroupService iCabinetGroupService;

    private final IStoreService iStoreService;

    private final IStoreAreaService iStoreAreaService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<CabinetGroupVo> initBean(@Validated(AddGroup.class) @RequestBody  CabinetGroupVo vo){
        //初始化仓库
//        List<StoreVo> storeVos = iStoreService.queryList(new StoreBo());
//        vo.setStoreId(storeVos.get(1).getId());

        if(StringUtils.isNotEmpty(vo.getCabinetType()) && vo.getCabinetType().equals("1")){
            List<StoreAreaVo> storeAreaVos = iStoreAreaService.queryList(new StoreAreaBo());
            List<StoreAreaVo> result = result = storeAreaVos.stream().filter(item->item.getAno().equals("CL")).collect(Collectors.toList());
            vo.setAreaId(result.get(0).getId());
        }
        vo.setStatus("0");
        vo.setLnums(0l);
        vo.setCnums(0l);
        vo.setPnums(0l);


        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询智能柜列表
     */
    @ApiOperation("查询智能柜列表")
//    @SaCheckPermission("wms:cabinetGroup:list")
    @GetMapping("/list")
    public TableDataInfo<CabinetGroupVo> list(@Validated(QueryGroup.class) CabinetGroupBo bo, PageQuery pageQuery) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        return iCabinetGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询库区列表
     */
    @ApiOperation("查询库区列表")
    @GetMapping("/cabinetGroupTree")
    public R<List<Tree<String>>> storeAreasTree(@Validated(QueryGroup.class) CabinetGroupBo bo) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        List<CabinetGroupVo> items = iCabinetGroupService.queryList(bo);
        return R.ok(iCabinetGroupService.buildCabinetGroupTreeSelect(items));
    }

    /**
     * 导出智能柜列表
     */
    @ApiOperation("导出智能柜列表")
    @SaCheckPermission("wms:cabinetGroup:export")
    @Log(title = "智能柜", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated CabinetGroupBo bo, HttpServletResponse response) {
        if(StringUtils.isEmpty(bo.getDelFlag())){
            bo.setDelFlag("0");
        }
        List<CabinetGroupVo> list = iCabinetGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "智能柜", CabinetGroupVo.class, response);
    }

    /**
     * 获取智能柜详细信息
     */
    @ApiOperation("获取智能柜详细信息")
//    @SaCheckPermission("wms:cabinetGroup:query")
    @GetMapping("/{id}")
    public R<CabinetGroupVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iCabinetGroupService.queryById(id));
    }

    /**
     * 新增智能柜
     */
    @ApiOperation("新增智能柜")
    @SaCheckPermission("wms:cabinetGroup:add")
    @Log(title = "智能柜", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CabinetGroupVo vo) {
        CabinetGroupBo bo = new CabinetGroupBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iCabinetGroupService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改智能柜
     */
    @ApiOperation("修改智能柜")
    @SaCheckPermission("wms:cabinetGroup:edit")
    @Log(title = "智能柜", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CabinetGroupVo vo) {
        CabinetGroupBo bo = new CabinetGroupBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iCabinetGroupService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除智能柜
     */
    @ApiOperation("删除智能柜")
    @SaCheckPermission("wms:cabinetGroup:remove")
    @Log(title = "智能柜", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iCabinetGroupService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
